$(function(){
    //1. 获取类别
    $.ajax({
        // 请求地址：/admin/category/list
        // 请求方式：get
        // 请求参数：无
        // 返回数据：文章
        url:'http://localhost:8080/api/v1/admin/category/list',
        dataType:'json',
        success:function(res){
            // console.log(res);
            if(res.code == 200){
                $('.category').html(template('category',res))
            }
        }

    })

    // 2. 日期插件
    jeDate("#articleDate",{
        format:"YYYY年MM月DD日",
        isTime:true,
        isToday:true,
        minDate:"2014-09-19 00:00:00",
        minDate: jeDate.nowDate({DD:0}), //0代表今天，-1代表昨天，-2代表前天，以此类推
    })

    //3. 富文本框
    tinymce.init({
        selector: '#mytextarea',
        language:'zh_CN',
        statusbar: false,
        height:300,
        directionality : 'ltr',
        browser_spellcheck: true,
        contextmenu: false,
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste imagetools wordcount",
            "code",
        ],
       
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code",
      });
    


      //文章封面图片
      $('#inputCover').on('change',function(){
          let myfile = this.files[0];
        //   console.log(myfile);
          let url = URL.createObjectURL(myfile);
        //   console.log(url);
        $('.article_cover').attr('src',url);
      })



    //   11、发布文章
    //    请求地址：/admin/article/publish
    //    请求方式：post
    //    请求参数：通过formData提交 title(文章标题) cover(文章封面图片) categoryId(文章类型id) date(日期) content(文章内容) state(已发布,草稿(为空默认为草稿))
    $('.btn-release').on('click',function(e){
        e.preventDefault();
        let formdata = new FormData($('#form')[0]);
        formdata.append('state','已发布');
        // console.log(formdata.get('title'));
        // console.log(formdata.get('cover'));
        // console.log(formdata.get('categoryId'));
        // console.log(formdata.get('date'));
        // console.log(formdata.get('state'));
        // console.log(formdata.get('content'));
        $.ajax({
            type:'post',
            url:'http://localhost:8080/api/v1/admin/article/publish',
            data:formdata,
            dataType:'json',
            contentType:false,
            processData:false,
            success:function(res){
                console.log(res);
                if(res.code == 200){
                    alert(res.msg);
                }
            }
        })
        
    })
      

    $('.btn-draft').on('click',function(e){
        e.preventDefault();
        let formdata = new FormData($('#form')[0]);
        // formdata.append('state','已发布');
        // console.log(formdata.get('title'));
        // console.log(formdata.get('cover'));
        // console.log(formdata.get('categoryId'));
        // console.log(formdata.get('date'));
        // console.log(formdata.get('state'));
        // console.log(formdata.get('content'));
        $.ajax({
            type:'post',
            url:'http://localhost:8080/api/v1/admin/article/publish',
            data:formdata,
            dataType:'json',
            contentType:false,
            processData:false,
            success:function(res){
                console.log(res);
                if(res.code == 200){
                    alert(res.msg);
                }
            }
        })
        
    })



})